/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jRPessoas.java
 *
 * Created on 14/06/2011, 02:56:40
 */

package sgfi;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import sgfi.Classes.ClassConecta;
import sgfi.Classes.Pessoas;

/**
 *
 * @author LKFalcao
 */
public class jRPessoas extends javax.swing.JFrame {

    /** Creates new form jRPessoas */
    public jRPessoas() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        imprimirjButton = new javax.swing.JButton();
        todosjCheckBox = new javax.swing.JCheckBox();
        sairjButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sgfi.SGFIApp.class).getContext().getResourceMap(jRPessoas.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), resourceMap.getColor("jPanel1.border.titleColor"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        imprimirjButton.setIcon(resourceMap.getIcon("imprimirjButton.icon")); // NOI18N
        imprimirjButton.setText(resourceMap.getString("imprimirjButton.text")); // NOI18N
        imprimirjButton.setName("imprimirjButton"); // NOI18N
        imprimirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imprimirjButtonActionPerformed(evt);
            }
        });

        todosjCheckBox.setText(resourceMap.getString("todosjCheckBox.text")); // NOI18N
        todosjCheckBox.setName("todosjCheckBox"); // NOI18N

        sairjButton.setIcon(resourceMap.getIcon("sairjButton.icon")); // NOI18N
        sairjButton.setText(resourceMap.getString("sairjButton.text")); // NOI18N
        sairjButton.setName("sairjButton"); // NOI18N
        sairjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(imprimirjButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                        .addComponent(sairjButton))
                    .addComponent(todosjCheckBox, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(todosjCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(imprimirjButton)
                    .addComponent(sairjButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sairjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairjButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_sairjButtonActionPerformed

    private void imprimirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imprimirjButtonActionPerformed
        // TODO add your handling code here:
try {
                Pessoas oPessoa = new Pessoas();

                String caminhoRelJasper = "resources/jRPessoas.jasper";

//                if (!todosjCheckBox.isSelected()){
//                oPessoa.setTipoCliente(tipoClientejComboBox.getSelectedItem().toString());
//                }

                ResultSet rs;

            try {

                rs = oPessoa.getConsultar();


                JRResultSetDataSource ds = new JRResultSetDataSource(rs);

                URL arquivo = getClass().getResource(caminhoRelJasper);

                if (arquivo == null)
                JOptionPane.showMessageDialog(null,"Arquivo jasper não encontrado");
                JasperReport report;
            try {

                    report = JasperManager.loadReport(arquivo.openStream());
                    JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap(), ds);
                    JasperViewer jrviewer = new JasperViewer(jasperPrint, false);
                    jrviewer.setVisible(true);


            } catch (JRException ex) {
                Logger.getLogger(jRPessoas.class.getName()).log(Level.SEVERE, null, ex);
            }

            } catch (IOException ex) {
                Logger.getLogger(jRPessoas.class.getName()).log(Level.SEVERE, null, ex);
            }


            } catch (SQLException ex) {
               Logger.getLogger(jRPessoas.class.getName()).log(Level.SEVERE, null, ex);
            }

    }//GEN-LAST:event_imprimirjButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        ClassConecta.conecta();
        
        this.setLocationRelativeTo(null);
    }//GEN-LAST:event_formWindowOpened

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jRPessoas().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton imprimirjButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton sairjButton;
    private javax.swing.JCheckBox todosjCheckBox;
    // End of variables declaration//GEN-END:variables

}
